<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>运动会</title>

    <link href="/css/common.css" rel="stylesheet">
    <link href="/css/index.css" rel="stylesheet">

    <% include common/main-lib.ejs %>
</head>
<body>

<div id="app">
  <el-container>
    <el-header>
      <% include common/header.ejs %>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <% include common/aside.ejs %>
      </el-aside>
      <el-container>
        <el-main>
          <h1><%= pageName %></h1>
        </el-main>
      </el-container>
    </el-container>
    <el-footer>田径运动会管理系统</el-footer>
  </el-container>
</div>

<script>
  new Vue({
    el: '#app',
    data () {
      return {
        arranges: [],
        arrangeList: [],
        scoreList: [],
        // finalGameList: [],
        eventList: []
      }
    },
    methods: {
      id2name(id) {
        let name = "";
        this.eventList.forEach(e => {
          if (e.eventId === id) {
            name = e.eventName;
          }
        })
        return name;
      },
      handleOpen(key, keyPath) {
        // console.log(key, keyPath);
      },
      handleClose(key, keyPath) {
        // console.log(key, keyPath);
      },
      getArrangeList() {
        return axios.get('/list/arrange');
      },
      getScoreList() {
        return axios.get('/list/score');
      },
      getEventList() {
        return axios.get('/list/event');
      },
      toEventList(response) {
        if (response.data.status === 200) {
          this.eventList = response.data.data;
        } else {
          console.log(error)
          this.$alert('服务器错误，请稍后重试', {
            confirmButtonText: '确定',
            type: 'error'
          })
        }
      },
      toArrangeList(response) {
        if (response.data.status === 200) {
          this.arrangeList = response.data.data;
        } else {
          console.log(error)
          this.$alert('服务器错误，请稍后重试', {
            confirmButtonText: '确定',
            type: 'error'
          })
        }
      },
      toScoreList(response) {
        if (response.data.status === 200) {
          this.scoreList = response.data.data;
        } else {
          console.log(error)
          this.$alert('服务器错误，请稍后重试', {
            confirmButtonText: '确定',
            type: 'error'
          })
        }
      },
      toArranges() {
        let _this = this;
        // 匹配成绩
        this.arranges = this.arrangeList.map(e => {
          let gameInfo = e.gameInfo;
          e.arrangeGroup.map(e1 => {
            e1.map(e2 => {
              if (gameInfo.gameType === "te") {
                let score = _this.id2score_te(e2.playerId, e2.trackEvent);
                return Object.assign(e2, {
                  score
                })
              } else if (gameInfo.gameType === "fe") {
                let score = _this.id2score_fe(e2.playerId, e2.fieldEvent);
                return Object.assign(e2, {
                  score
                })
              }
            })
          })

          return e
        })

        // console.log(this.arranges)
      },
      id2score_fe(playerId, fieldEvent) {
        let score = "";
        this.scoreList.forEach(e => {
          if (e.fieldEvent === fieldEvent && e.playerId === playerId) {
            score = e.score;
          }
        })
        return score;
      },
      id2score_te(playerId, trackEvent) {
        let score = "";
        this.scoreList.forEach(e => {
          if (e.trackEvent === trackEvent && e.playerId === playerId) {
            score = e.score;
          }
        })
        return score;
      },
      getGameName(eventId, sex, status) {
        let sexName = sex === "1" ? "男子组" : "女子组";
        let statusName = status === "0" ? "初赛" : "决赛";
        let projectName = this.id2name(eventId);
        return sexName + "：" + projectName + " " + statusName;
      },
      enterMark(e) { // 决赛录入成绩
        let finalGame = JSON.parse(JSON.stringify(e));

        let _this = this;
        let gameInfo = finalGame.gameInfo;
        // 取前三名
        let group = finalGame.arrangeGroup[0].splice(0, 3);

        if (gameInfo.gameType === "fe") {
          group.sort((a,b) => {
            return b.score - a.score;
          })
        } else if (gameInfo.gameType === "te") {
          group.sort((a,b) => {
            return a.score - b.score;
          })
        }
        group[0].playerMark = gameInfo.gameMark[0]
        group[1].playerMark = gameInfo.gameMark[1]
        group[2].playerMark = gameInfo.gameMark[2]

        axios.post('/enterPlayerMark', {
          gameId: gameInfo.gameId,
          players: group
        })
          .then(function (response) {
            if (response.data.status === 200) {
              _this.$message.success(response.data.msg);
              e.gameInfo.gameStatus = "4"  // 表示已录入成绩
            } else {
              throw new Error(response.data.msg)
            }
          })
          .catch(function (error) {
            console.log(error)
            _this.$alert(error.message, {
              confirmButtonText: '确定',
              type: 'error'
            })
          });
      },
      groupRanking(e) {  // 分组排名
        let _this = this;
        let gameInfo = e.gameInfo;
        let group = []

        if (gameInfo.gameType === "fe") {
          e.arrangeGroup.map(e1 => {
            e1.sort((a,b) => {
              return b.score - a.score;
            })
            group.push(e1[0])
          })
        } else if (gameInfo.gameType === "te") {
          e.arrangeGroup.map(e1 => {
            e1.sort((a,b) => {
              return a.score - b.score;
            })
            group.push(e1[0])
          })
        }
        return group;
      },
      toFinalGroup(e) {  // 生成决赛
        let _this = this;
        let gameInfo = e.gameInfo;
        if (gameInfo.gameStatus === "0") {

          let group = _this.groupRanking(e)

          let finalGameInfo = {
            gameId: gameInfo.gameSex + "1" + gameInfo.eventId,
            gameName: _this.getGameName(gameInfo.eventId, gameInfo.gameSex, "1"),
            gameStatus: "1",
            groupNum: group.length,
            ready: gameInfo.ready,
            desc: gameInfo.desc,
            eventId: gameInfo.eventId,
            gameField: gameInfo.gameField,
            gameSex: gameInfo.gameSex,
            gameType: gameInfo.gameType,
            judgeId: gameInfo.judgeId,
            time0: gameInfo.time0,
            time1: gameInfo.time1,
            group,
            gameMark: gameInfo.gameMark,
            game0id: gameInfo.gameId
          }

          // this.finalGameList.push(finalGameInfo);

          // console.log(finalGameInfo)

          axios.post('/newGame', finalGameInfo)
            .then(function (response) {
              if (response.data.status === 200) {
                _this.$message.success(response.data.msg);
                e.gameInfo.gameStatus = "3"  // 表示已安排决赛
              } else {
                throw new Error(response.data.msg)
              }
            })
            .catch(function (error) {
              console.log(error)
              _this.$alert(error.message, {
                confirmButtonText: '确定',
                type: 'error'
              })
            });
        }
      },
      // toFinalList() {
      //   let _this = this;
      //   this.arranges = this.arrangeList.forEach(e => {
      //     _this.toFinalGroup(e);
      //   })
      // }
    },
    created() {
      let _this = this;
      axios.all([_this.getArrangeList(), _this.getScoreList(), _this.getEventList()])
        .then(axios.spread(function (arrangeList, scoreList, eventList) {
          _this.toArrangeList(arrangeList);
          _this.toScoreList(scoreList);
          _this.toEventList(eventList);
          _this.toArranges();
        }));
    }
  })
</script>
</body>
</html>
